﻿using System;
using System.Text;

namespace MockingBird.Simulator.Framework.Exceptions
{
    /// <summary>
    /// Reports all exception conditions to do with the structure and data of the handler configuration 
    /// For example, setting a ResponseStrategy of default and leaving the file name empty is a HandlerConfiguration Exception
    /// </summary>
    public class HandlerConfigurationException : Exception
    {
        string message;

        public HandlerConfigurationException(string message)
            : base()
        {
            this.message = message;
        }

        public HandlerConfigurationException(string message, params string[] args)
            : base()
        {
            this.message = string.Format(message, args);
        }

        public HandlerConfigurationException(Exception innerException, string message)
            : base(message, innerException)
        {
            this.message = message;
        }

        public HandlerConfigurationException(Exception innerException, string message, params string[] args)
            : base(message, innerException)
        {
            this.message = string.Format(message, args);
        }

        public override string Message
        {
            get
            {
                return message;
            }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Exception type: " + this.GetType());
            sb.AppendLine("Message: " + this.message);
            if (this.InnerException != null)
            {
                sb.AppendLine(GetInnerExceptionMessage(this.InnerException, 1));
            }

            return base.ToString();
        }

        private string GetInnerExceptionMessage(Exception ex, int nestingLevel)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("There was an inner exception of type: " + ex.GetType());
            sb.AppendLine("Message: " + ex.Message);
            if (ex.InnerException != null)
            {
                if (nestingLevel < 9) sb.AppendLine(GetInnerExceptionMessage(ex.InnerException, nestingLevel + 1));
                else sb.AppendLine("Limit of inner exception reporting reached.");
            }
            return sb.ToString();
        }


    }

}
